import Taro from '@tarojs/taro';
import { getCountryForCode } from '../components/CountryPicker/tool';
import templateConfig from '../config/template';
import language from '../models/language';
import { Localization } from '../utils';
import AsyncStorage from '../utils/AsyncStorage';

export const serviceMap: {
  [key: string]: string;
} = {
  CN: 'cn',
  JP: 'us',
  US: 'us',
  CA: 'us',
  GB: 'eu',
  FR: 'eu',
  DE: 'eu',
  DZ: 'eu',
  AF: 'us',
  AR: 'us',
  AE: 'us',
  SY: 'us',
  AW: 'us',
  OM: 'us',
  AZ: 'us',
  EG: 'eu',
  ET: 'eu',
  IE: 'eu',
  EE: 'eu',
  AD: 'eu',
  AO: 'eu',
  AI: 'us',
  AG: 'us',
  AT: 'eu',
  AX: 'eu',
  AU: 'us',
  MO: 'us',
  BB: 'us',
  PG: 'us',
  BS: 'us',
  PK: 'us',
  PY: 'us',
  PS: 'us',
  BH: 'us',
  PA: 'us',
  BR: 'us',
  BY: 'eu',
  BM: 'us',
  BG: 'eu',
  MP: 'us',
  BJ: 'eu',
  BE: 'eu',
  IS: 'us',
  PR: 'us',
  PL: 'eu',
  BA: 'eu',
  BO: 'us',
  BZ: 'us',
  BW: 'eu',
  BQ: 'us',
  BT: 'us',
  BF: 'eu',
  BI: 'eu',
  KP: 'us',
  GQ: 'eu',
  DK: 'eu',
  TL: 'us',
  TG: 'eu',
  DM: 'us',
  DO: 'us',
  RU: 'us',
  EC: 'us',
  ER: 'eu',
  FO: 'us',
  PF: 'us',
  GF: 'us',
  TF: 'eu',
  PH: 'us',
  FJ: 'us',
  FI: 'eu',
  CV: 'us',
  FK: 'us',
  GM: 'eu',
  CG: 'eu',
  CD: 'eu',
  CO: 'us',
  CR: 'us',
  GD: 'us',
  GL: 'us',
  GE: 'us',
  GG: 'eu',
  CU: 'us',
  GP: 'us',
  GU: 'us',
  GY: 'us',
  KZ: 'us',
  HT: 'us',
  KR: 'us',
  NL: 'eu',
  ME: 'eu',
  HN: 'us',
  TR: 'us',
  KI: 'us',
  DJ: 'eu',
  KG: 'us',
  GN: 'eu',
  GW: 'eu',
  GH: 'eu',
  GA: 'eu',
  KH: 'us',
  VA: 'eu',
  CZ: 'eu',
  ZW: 'eu',
  CM: 'eu',
  QA: 'us',
  KY: 'us',
  CC: 'eu',
  KM: 'eu',
  CI: 'eu',
  KW: 'us',
  HR: 'eu',
  KE: 'eu',
  CK: 'us',
  CW: 'us',
  LV: 'eu',
  LS: 'eu',
  LA: 'us',
  LB: 'us',
  LT: 'eu',
  LR: 'eu',
  LY: 'eu',
  LI: 'eu',
  LU: 'eu',
  RW: 'eu',
  RO: 'eu',
  MG: 'eu',
  IM: 'eu',
  MV: 'eu',
  MT: 'eu',
  MW: 'eu',
  MY: 'us',
  ML: 'eu',
  MK: 'eu',
  MH: 'us',
  MQ: 'us',
  YT: 'eu',
  MU: 'eu',
  MR: 'eu',
  UM: 'us',
  AS: 'us',
  VI: 'us',
  MN: 'us',
  MS: 'us',
  BD: 'us',
  PE: 'us',
  FM: 'us',
  MM: 'us',
  MD: 'eu',
  MA: 'eu',
  MC: 'eu',
  MZ: 'eu',
  MX: 'us',
  NA: 'eu',
  ZA: 'eu',
  AQ: 'us',
  GS: 'us',
  SS: 'eu',
  NR: 'us',
  NP: 'us',
  NI: 'us',
  NE: 'eu',
  NG: 'eu',
  NU: 'us',
  NO: 'eu',
  NF: 'us',
  PW: 'us',
  PN: 'us',
  PT: 'us',
  SE: 'eu',
  CH: 'eu',
  EH: 'eu',
  SV: 'us',
  WS: 'us',
  RS: 'eu',
  SL: 'eu',
  SN: 'eu',
  CY: 'us',
  SC: 'eu',
  SA: 'us',
  BL: 'us',
  CX: 'eu',
  ST: 'eu',
  SH: 'us',
  KN: 'us',
  LC: 'us',
  MF: 'us',
  SX: 'us',
  SM: 'eu',
  PM: 'us',
  VC: 'us',
  LK: 'us',
  SK: 'eu',
  SI: 'eu',
  SJ: 'us',
  SZ: 'eu',
  SD: 'eu',
  SR: 'us',
  SB: 'us',
  SO: 'eu',
  TJ: 'us',
  TW: 'us',
  TH: 'us',
  TZ: 'eu',
  TO: 'us',
  TC: 'us',
  TT: 'us',
  TN: 'eu',
  TV: 'us',
  TM: 'us',
  RE: 'cn',
  TK: 'us',
  WF: 'us',
  VU: 'us',
  GT: 'us',
  VE: 'us',
  BN: 'us',
  UG: 'eu',
  UA: 'eu',
  UY: 'us',
  UZ: 'us',
  ES: 'eu',
  GR: 'eu',
  HK: 'us',
  SG: 'us',
  NC: 'us',
  NZ: 'us',
  JE: 'eu',
  HU: 'eu',
  JM: 'us',
  AM: 'us',
  YE: 'us',
  IQ: 'us',
  IR: 'us',
  IL: 'us',
  IT: 'eu',
  IN: 'us',
  ID: 'us',
  VG: 'us',
  IO: 'eu',
  JO: 'us',
  VN: 'us',
  ZM: 'eu',
  TD: 'eu',
  GI: 'eu',
  CL: 'us',
  CF: 'eu',
};

const defaultServeKey = 'cn';

class ServiceInfoManager {
  serve: { [key: string]: IServiceInfo } = {
    cn: {
      openAPIInfo: 'api.gizwits.com', // 生产
      siteInfo: 'site.gizwits.com', // 生产
      aepInfo: 'https://aep-app.gizwits.com/', // 生产
      uploadApiUrl: 'https://aep-backend.gizwits.com/file/', // 生产
      gatewayInfo: 'https://api.gizwitsapi.com/', // 生产
      gwApplicationUrl: 'https://developer.gizwits.com/siteApi/v1', // 生产
      oAuthInfo: 'https://oauth.gizwitsapi.com/',

      cybInfo: 'cybertron.gizwits.com',
      uploadClient: 'Qcloud_cos',
      pushInfo: 'push.gizwitsapi.com',

      // openAPIInfo: 'api2.iotsdk.com', // 测试
      // siteInfo: 'site2.iotsdk.com', // 测试
      // aepInfo: 'https://backend-demo.lease.iotsdk.com/backend/', // 测试
      // uploadApiUrl: 'http://backend-demo.lease.iotsdk.com/file/', // 测试
      // gatewayInfo: 'https://api-aep.iotsdk.com/', // 测试
      // gwApplicationUrl: 'http://developer2.iotsdk.com/siteApi/v1',
      // oAuthInfo: 'http://app-demo1.lease.iotsdk.com/',
    },
    us: {
      openAPIInfo: 'usapi.gizwits.com',
      siteInfo: 'ussite.gizwits.com',
      pushInfo: 'us.push.gizwitsapi.com',
      aepInfo: 'https://usaepapp.gizwits.com/', // 生产
      uploadApiUrl: 'https://usaepbk.gizwits.com/file/', // 生产
      gwApplicationUrl: 'https://developer.gizwits.com/siteApi/v1', // 生产
      // aepInfo: 'http://app-demo1.lease.iotsdk.com/',  // 测试
      // uploadApiUrl: 'http://backend-demo.lease.iotsdk.com/file/', // 测试
      cybInfo: 'cybertron.gizwits.com',
      uploadClient: 'Amazon_S3',
      oAuthInfo: 'https://oauth.gizwitsapi.com/',
      // gatewayInfo: 'https://api-aep.iotsdk.com/', // 测试
      gatewayInfo: 'https://usapi.gizwitsapi.com/', // 生产
    },
    eu: {
      openAPIInfo: 'euapi.gizwits.com',
      siteInfo: 'eusite.gizwits.com',
      pushInfo: 'eu.push.gizwitsapi.com',
      aepInfo: 'https://euaepapp.gizwits.com/', // 生产
      uploadApiUrl: 'https://euaepbk.gizwits.com/file/', // 生产
      gwApplicationUrl: 'https://developer.gizwits.com/siteApi/v1', // 生产
      // aepInfo: 'http://app-demo1.lease.iotsdk.com/',  // 测试
      // uploadApiUrl: 'http://backend-demo.lease.iotsdk.com/file/', // 测试
      cybInfo: 'cybertron.gizwits.com',
      uploadClient: 'Amazon_S3',
      oAuthInfo: 'https://oauth.gizwitsapi.com/',
      // gatewayInfo:'https://api-aep.iotsdk.com/', // 测试
      gatewayInfo: 'https://euapi.gizwitsapi.com/', // 生产
    },
  };

  serviceMap = serviceMap;

  /**
   * 不指定服务器地址
   * 则从本地获取国家信息，再判断
   */
  getServiceInfo = async (serve?: string): Promise<IServiceInfo> => {
    /**
     * 需要检查配置文件是否强制指定了服务器地址
     * 如果指定了，则直接写死
     */
    if (templateConfig.app && templateConfig.app.cloudServiceInfo) {
      const serveInfo = templateConfig.app.cloudServiceInfo;
      if (serveInfo instanceof Object) {
        return new Promise(res => {
          res(serveInfo);
        });
      }
    }
    if (serve) {
      return Promise.resolve(this.serve[this.serviceMap[serve.toUpperCase()] || defaultServeKey]);
    }
    // 从本地存储获取当前国家信息，再判断
    const { countryCode } = await this.getCurrentCountry();
    return this.getServiceInfo(countryCode) || this.serve.us;
  };

  getServiceInfoSync = (serve?: string): IServiceInfo => {
    /**
     * 需要检查配置文件是否强制指定了服务器地址
     * 如果指定了，则直接写死
     */
    if (templateConfig.app && templateConfig.app.cloudServiceInfo) {
      const serveInfo = templateConfig.app.cloudServiceInfo;
      if (serveInfo instanceof Object) {
        return serveInfo;
      }
    }
    if (serve) {
      return this.serve[this.serviceMap[serve.toUpperCase()] || defaultServeKey];
    }
    // 从本地存储获取当前国家信息，再判断
    const { countryCode } = this.getCurrentCountrySync();
    return this.getServiceInfoSync(countryCode) || this.serve.us;
  };

  getServiceKey = async () => {
    const { countryCode } = await this.getCurrentCountry();
    const serve = this.serviceMap[countryCode.toUpperCase()] || defaultServeKey;
    return serve;
  };

  /**
   * 获取当前国家
   * 存在countryCode就用countryCode来判断
   * 不存在则用语言
   */
  getCurrentCountry = async () => {
    const countryCode: string = (await AsyncStorage.getItem('countryCode')) || Localization.country();
    const phoneCode =
      (await AsyncStorage.getItem('phoneCode')) || getCountryForCode(language.key, Localization.country())?.phoneCode;
    return { phoneCode, countryCode };
  };

  getCurrentCountrySync = () => {
    const countryCode: string = Taro.getStorageSync('countryCode') || Localization.country();
    const phoneCode =
      Taro.getStorageSync('phoneCode') || getCountryForCode(language.key, Localization.country())?.phoneCode;
    return { phoneCode, countryCode };
  };

  initCountry = async () => {
    const { phoneCode, countryCode } = await this.getCurrentCountry();
    console.log(phoneCode, countryCode);
    const localPhoneCode = await AsyncStorage.getItem('phoneCode');
    if (!localPhoneCode) {
      await AsyncStorage.setItem('phoneCode', phoneCode);
    }
    const localCountryCode = await AsyncStorage.getItem('countryCode');
    if (!localCountryCode) {
      await AsyncStorage.setItem('countryCode', countryCode);
    }
  };

  // 获取openApi的地址
  getOpenApiUrl = async (serve?: string) => {
    const serveinfo = await this.getServiceInfo(serve);
    return `https://${serveinfo.openAPIInfo}/`;
  };

  getSiteApiUrl = async (serve?: string) => {
    const serveinfo = await this.getServiceInfo(serve);
    return `https://${serveinfo.siteInfo}/`;
  };

  getCozeApiUrl = async (serve?: string) => {
    return 'https://agent.gizwitsapi.com/';
    // return `http://172.16.11.46:8080/`;

  };
  getAepApiUrl = async (serve?: string) => {
    const serveinfo = await this.getServiceInfo(serve);
    return serveinfo.aepInfo;
  };

  getAepApiUrlSync = (serve?: string) => {
    const serveinfo = this.getServiceInfoSync(serve);
    return serveinfo.aepInfo;
  };

  getGatewayURl = async (serve?: string) => {
    const serveinfo = await this.getServiceInfo(serve);
    return serveinfo.gatewayInfo;
  };

  getOAuthUrl = async (serve?: string) => {
    const serveinfo = await this.getServiceInfo(serve);
    return serveinfo.oAuthInfo;
  };

  getCybApiUrl = async (serve?: string) => {
    const serveinfo = await this.getServiceInfo(serve);
    return `https://${serveinfo.cybInfo}/`;
  };

  uploadApiUrl = async (serve?: string) => {
    const serveinfo = await this.getServiceInfo(serve);
    return `${serveinfo.uploadApiUrl}file/singleUpload?repo=aep&clientCode=${serveinfo.uploadClient}`;
  };
  getPaymentApiUrl = async () => {
    return 'https://agent.gizwitsapi.com/';
    // return `http://172.16.11.46:8080/`;
  };

  getServiceMap = () => {
    return serviceMap;
  };

  getGwApplicationUrl = async (serve?: string) => {
    const serveinfo = await this.getServiceInfo(serve);
    return serveinfo.gwApplicationUrl;
  };
}

export default new ServiceInfoManager();
